package kursynbp;

import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;

import org.junit.BeforeClass;
import org.junit.Test;

public class KursyNbpTest {

	private static KursyNBP kursy;
	private static Date from, to;
	
	@BeforeClass
	public static void init() {
		kursy = new KursyNBP();
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, 2014);
		cal.set(Calendar.MONTH, 9);
		cal.set(Calendar.DAY_OF_MONTH, 29);
		from = (Date) cal.getTime();
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(Calendar.YEAR, 2014);
		cal2.set(Calendar.MONTH, 9);
		cal2.set(Calendar.DAY_OF_MONTH, 30);
		to = (Date) cal.getTime();
		
	}
	
	@Test
	public void shouldFindLowestCurrencyRateTest() {
		//given
		BigDecimal expectedValue = new BigDecimal(0.073).setScale(3, RoundingMode.HALF_DOWN);
		
		//when
		BigDecimal valueFromDb = kursy.getLowestExchangeRate(from).getValue();
		
		//then
		assertEquals(expectedValue, valueFromDb) ;
	}

	@Test
	public void shouldFindHighestCurrencyRateTest() {
		// given
		BigDecimal expectedValue = new BigDecimal(5.364).setScale(3, RoundingMode.HALF_DOWN);
		
		// when
		BigDecimal valueFromDb = kursy.getHighestExchangeRate(from).getValue();
		
		// then
		assertEquals(expectedValue, valueFromDb);
	}
	
	@Test
	public void shouldFindLowestCurrencyRateInPeriodTest() {
		
		// given
		Exchange currency = new Exchange("USD");
		BigDecimal expectedValue = new BigDecimal(3.300).setScale(3, RoundingMode.HALF_DOWN);

		// when
		BigDecimal valueFromDb = kursy.getLowestExchangeRate(currency, from, to).getValue();
		
		// then
		assertEquals(expectedValue, valueFromDb);
	}

	@Test
	public void shouldFindHighestCurrencyRateInPeriodTest() {
		
		// given
		Exchange currency = new Exchange("AUD");
		BigDecimal expectedValue = new BigDecimal(2.877).setScale(3, RoundingMode.HALF_DOWN);

		// when
		BigDecimal valueFromDb = kursy.getHighestExchangeRate(currency, from, to).getValue();
		
		// then
		assertEquals(expectedValue, valueFromDb);
	}
	
	@Test
	public void shouldFindCurrencyWithBiggestDiffInPeriodTest() {
		
		// given
		String expectedValue = "XDR";
		
		// when
		String valueFromDb = kursy.getBiggestExchangeDiff(from, to).getCurrency().getCode();
		
		// then
		assertEquals(expectedValue, valueFromDb);
	}
	
	@Test
	public void shouldFindBiggestCurrencyDiffInPeriodTest() {
		
		// given
		BigDecimal expectedValue = new BigDecimal(1.578).setScale(3, RoundingMode.HALF_DOWN);
		
		// when
		BigDecimal valueFromDb = kursy.getBiggestExchangeDiff(from, to).getValue();
		
		// then
		assertEquals(expectedValue, valueFromDb);
	}
}
